﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.IO.Compression;

namespace UploadBlobToAzure
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            string currentDirectory = Directory.GetCurrentDirectory();
            bool azcopyFound = File.Exists("azcopy.exe");
            bool storagedllFound = File.Exists("Microsoft.WindowsAzure.Storage.dll");
            bool storageDataMovementFound = File.Exists("Microsoft.WindowsAzure.Storage.DataMovement.dll");
            bool azcopyzipfound = File.Exists("azcopy.zip");

            if (!azcopyFound || !storagedllFound || !storageDataMovementFound)
            {
                if (azcopyzipfound)
                {
                    File.Delete("azcopy.zip");
                }
                if (Directory.Exists("azcopy"))
                {
                    Directory.Delete("azcopy", true);
                } 
                if(azcopyFound)
                {
                    File.Delete("azcopy.exe");
                }
                if (storagedllFound)
                {
                    File.Delete("Microsoft.WindowsAzure.Storage.dll");
                }
                if (storageDataMovementFound)
                {
                    File.Delete("Microsoft.WindowsAzure.Storage.DataMovement.dll");
                }

                MessageBox.Show("There is No Azcopy in your present folder,we will now download the azcopy , click OK to proceed");
                
                try
                {
                    WebClient Client = new WebClient();
                    Client.DownloadFile("https://github.com/downloads/WindowsAzure/azure-sdk-downloads/AzCopy.zip", "AzCopy.zip");
                    ZipFile.ExtractToDirectory("azcopy.zip", "azcopy");
                    foreach (var file in Directory.EnumerateFiles("azcopy"))
                    {
                        File.Move(file, currentDirectory + "\\" + Path.GetFileName(file));
                    }
                    Directory.Delete("azcopy");
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);                    
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                //Application.Run(new UploadBlob());
            }


        }
    }
}
